<?php
// +----------------------------------------------------------------------
// | Tplay [ WE ONLY DO WHAT IS NECESSARY ]
// +----------------------------------------------------------------------
// | Copyright (c) 2017 http://tplay.pengyichen.com All rights reserved.
// +----------------------------------------------------------------------
// | Licensed ( http://www.apache.org/licenses/LICENSE-2.0 )
// +----------------------------------------------------------------------
// | Author: 听雨 < 389625819@qq.com >
// +----------------------------------------------------------------------


namespace app\admin\controller;







use \think\Cache;
use \think\Controller;
use think\Loader;
use \think\Session;
use think\Db;
use \think\Cookie;
use app\admin\controller\User;
use app\admin\model\Huan as HuanModel;
use app\admin\model\Typal as TtpalModel;
class Huan extends Permissions
{
    public function index()
    {
        $model = new HuanModel();
        $post = $this->request->param();
        if (isset($post['keywords']) and !empty($post['keywords'])) {
            $where['name'] = ['like', '%' . $post['keywords'] . '%'];
        }
        if (isset($post['typal_id']) and $post['typal_id'] > 0) {
            $where['typal_id'] = $post['typal_id'];
        }

        if (isset($post['admin_id']) and $post['admin_id'] > 0) {
            $where['admin_id'] = $post['admin_id'];
        }
        
        if (isset($post['status']) and ($post['status'] == 1 or $post['status'] === '0')) {
            $where['status'] = $post['status'];
        }

        if(isset($post['create_time']) and !empty($post['create_time'])) {
            $min_time = strtotime($post['create_time']);
            $max_time = $min_time + 24 * 60 * 60;
            $where['create_time'] = [['>=',$min_time],['<=',$max_time]];
        }
        
        $huan = empty($where) ? $model->order('create_time desc')->paginate(20) : $model->where($where)->order('create_time desc')->paginate(20,false,['query'=>$this->request->param()]);
       // print_r($huan);die;
      //json($huan);die;
        //$articles = $article->toArray();
        //添加最后修改人的name
        foreach ($huan as $key => $value) {
            $huan[$key]['edit_admin'] = Db::name('admin')->where('id',$value['edit_admin_id'])->value('nickname');
        }

        $this->assign('huan',$huan);
        $info['admin'] = Db::name('admin')->select();
        $info['typal'] = Db::name('typal')->select();
        $this->assign('info',$info);
        return $this->fetch();
    }

/**
 * 数据删除
 * 
 */

public function delete()
{
    if($this->request->isAjax()) {
        $model = new HuanModel();
        $id = $this->request->has('id') ? $this->request->param('id', 0, 'intval') : 0;
        if(false == $model->where('id',$id)->delete()) {
            return $this->error('删除失败');
        } else {
            addlog($id);//写入日志
            return $this->success('删除成功','admin/huan/index');
        }
    }
}


/**
 * 数据添加/修改
 */
public function publish()
    {
    	//获取菜单id
    	$id = $this->request->has('id') ? $this->request->param('id', 0, 'intval') : 0;
        $model = new HuanModel();
        $TtpalModel = new TtpalModel();
		//是正常添加操作
		if($id > 0) {
    		//是修改操作
    		if($this->request->isPost()) {
    			//是提交操作
    			$post = $this->request->post();
    			//验证  唯一规则： 表名，字段名，排除主键值，主键名
	            $validate = new \think\Validate([
                    ['name', 'require', '幻灯片地址不能为空'],
                    ['typal_id', 'require', '分类不能为空'],
                    ['url', 'require', 'URL不能为空'],
                    ['attid', 'require', '图片不能为空'],
	            ]);
	            //验证部分数据合法性
	            if (!$validate->check($post)) {
	                $this->error('提交失败：' . $validate->getError());
	            }
	            //验证菜单是否存在
	            $article = $model->where('id',$id)->find();
	            if(empty($article)) {
	            	return $this->error('id不正确');
	            }
                //设置修改人
                $post['edit_admin_id'] = Session::get('admin');
	            if(false == $model->allowField(true)->save($post,['id'=>$id])) {
	            	return $this->error('修改失败');
	            } else {
                    addlog($model->id);//写入日志
	            	return $this->success('修改成功','admin/huan/index');
	            }
    		} else {
    			//非提交操作
                $huan = Db::name('huan')->where('id',$id)->find();
                $info['typal'] = Db::name('typal')->select();
                $typal = $TtpalModel->select();
    			$this->assign('typal',$typal);
    			if(!empty($huan)) {
                    $this->assign('huan',$huan);
    				return $this->fetch();
    			} else {
    				return $this->error('id不正确');
    			}
    		}
    	} else {
    		//是新增操作
    		if($this->request->isPost()) {
    			//是提交操作
    			$post = $this->request->post();
    			//验证  唯一规则： 表名，字段名，排除主键值，主键名
	            $validate = new \think\Validate([
                    ['name', 'require', '幻灯片地址不能为空'],
                    ['typal_id', 'require', '分类不能为空'],
                    ['url', 'require', '连接不能为空'],
                    ['attid', 'require', '图片不能为空'],
	            ]);
	            //验证部分数据合法性
	            if (!$validate->check($post)) {
	                $this->error('提交失败：' . $validate->getError());
	            }
                //设置创建人
                $post['admin_id'] = Session::get('admin');
                //设置修改人
                $post['edit_admin_id'] = $post['admin_id'];
	            if(false == $model->allowField(true)->save($post)) {
	            	return $this->error('添加失败');
	            } else {
                    addlog($model->id);//写入日志
	            	return $this->success('添加成功','admin/huan/index');
	            }
    		} else {
    			//非提交操作
                $info['typal'] = Db::name('typal')->select();
                $this->assign('info',$info);
    			return $this->fetch();
    		}
    	}
    	
    }


    public function status()
    {
        if($this->request->isPost()){
            $model = new HuanModel();
            $post = $this->request->post();
            if(false == $model->where('id',$post['id'])->update(['status'=>$post['status']])) {
                return $this->error('设置失败');
            } else {
                addlog($post['id']);//写入日志
                return $this->success('设置成功','admin/site/index');
            }
        }
    }
}
